// ==UserScript==
// @name         微信懒人翻书
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  实现微信读书自动翻书功能
// @author       yuankaiyu
// @match        https://weread.qq.com/*
// @icon         
// @grant        none
// @license      MIT
// @downloadURL https://update.greasyfork.org/scripts/492212/%E5%BE%AE%E4%BF%A1%E6%87%92%E4%BA%BA%E7%BF%BB%E4%B9%A6.user.js
// @updateURL https://update.greasyfork.org/scripts/492212/%E5%BE%AE%E4%BF%A1%E6%87%92%E4%BA%BA%E7%BF%BB%E4%B9%A6.meta.js
// ==/UserScript==

(function () {
  "use strict";
  const box = document.createElement("div");
  const divEle = document.createElement("div");
  var textNode = document.createTextNode("懒人翻书");
  divEle.appendChild(textNode);
  box.style.position = "fixed";
  box.style.top = "24px";
  box.style.left = "600px";
  box.style.fontSize = "18px";
  box.style.background = "rgb(48, 173, 254)";
  box.style.padding = "10px";
  box.style.borderRadius = "5px";
  box.style.boxShadow = "4px 4px 20px rgba(0, 0, 0, 0.4)";
  divEle.addEventListener("click", function (event) {
    scrollAuto();
  });
  divEle.onclick = "scrollAuto()";
  box.appendChild(divEle);
  const speedController = document.createElement("div");
  speedController.id = "speedController";
  const minusBtn = document.createElement("button");
  minusBtn.innerText = "-";
  minusBtn.id = "minusBtn";
  minusBtn.style.background = "white";
  minusBtn.style.width = "18px";
  minusBtn.style.height = "18px";
  minusBtn.style.lineHeight = "17px";
  minusBtn.style.marginRight = "5px";
  minusBtn.style.borderRadius = "5px";
  minusBtn.style.cursor = "pointer";
  minusBtn.addEventListener("click", function (event) {
    minus();
  });
  const speedEle = document.createElement("span");
  speedEle.id = "speed";
  speedEle.innerText = "0.5";
  const addBtn = document.createElement("button");
  addBtn.innerText = "+";
  addBtn.id = "addBtn";
  addBtn.style.background = "white";
  addBtn.style.width = "18px";
  addBtn.style.height = "18px";
  addBtn.style.lineHeight = "17px";
  addBtn.style.marginLeft = "5px";
  addBtn.style.borderRadius = "5px";
  addBtn.style.cursor = "pointer";
  addBtn.addEventListener("click", function (event) {
    add();
  });
  speedController.appendChild(minusBtn);
  speedController.appendChild(speedEle);
  speedController.appendChild(addBtn);
  box.appendChild(speedController);
  const hint1 = document.createElement("div");
  hint1.innerText = "Min 0.1";
  hint1.style.cursor = "pointer";
  hint1.addEventListener("click", function (event) {
    speedEle.innerText = 0.1;
  });
  const hint2 = document.createElement("div");
  hint2.innerText = "Max 2";
  hint2.style.cursor = "pointer";
  hint2.addEventListener("click", function (event) {
    speedEle.innerText = 2;
  });
  box.appendChild(hint1);
  box.appendChild(hint2);
  document.body.appendChild(box);
  let animationFrameId;
  let scrollPosition = 0;
  const scrollSpeed = 20; // 你可以调整这个值来改变滚动速度
  // 为元素添加keydown事件监听器
  document.addEventListener("keydown", function (event) {
    // 检查按下的键是否是空格键
    if (event.key === " " || event.keyCode === 32) {
      event.preventDefault();
      scrollAuto();
    }
  });

  function scrollAuto() {
    const scrollDistance = +speedEle.innerText; // 每次滚动的距离
    const renderTargetContainer = document.querySelector(
      ".renderTargetContainer"
    );
    scrollPosition = window.scrollY;
    // 空格键被按下，执行相应的操作
    if (animationFrameId) {
      animationFrameId = cancelAnimationFrame(animationFrameId);
    } else {
      // 在这里你可以添加你想要执行的代码
      animationFrameId = requestAnimationFrame(smoothScroll);
    }
    function smoothScroll() {
      scrollPosition += scrollDistance;
      window.scrollTo(0, scrollPosition);
      // 当你想要停止滚动时，清除这个间隔
      if (isScrolledToBottom()) {
        console.log("滚动到底部了");
        cancelAnimationFrame(animationFrameId);
      }
    }
  }
  function isScrolledToBottom() {
    return window.scrollY + window.innerHeight >= document.body.scrollHeight;
  }

  const minus = () => {
    let speed = +speedEle.innerText;
    speed -= 0.1;
    speed = Math.round(speed * 100) / 100;
    speedEle.innerText = speed;
  };

  const add = () => {
    let speed = +speedEle.innerText;
    speed += 0.1;
    speed = Math.round(speed * 100) / 100;
    speedEle.innerText = speed;
  };
  // Your code here...
})();
